package gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import security.Encryption;

import entities.User;

public class Login {
	private WindowsApplication windowsApp;
	private JPanel pnlLogin;
	private JLabel lblLoginPage;
	private JLabel lblUsername;
	private JLabel lblPassword;
	private JPasswordField txtPassword;
	private JTextField txtUsername;
	
	public Login(){
		initialize();
	}
	public Login(WindowsApplication windowsApp){
		this.windowsApp = windowsApp;
		initialize();
	}
	private void initialize(){
		pnlLogin = new JPanel();
		pnlLogin.setBounds(0, 0, 800, 600);
		pnlLogin.setLayout(null);
	
		lblLoginPage = new JLabel("Please log in to continue. If you don't have an account, please register.");
		lblLoginPage.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 20));
		lblLoginPage.setBounds(50, 25, 700, 137);
		pnlLogin.add(lblLoginPage);
		
		if(windowsApp.getErrors().size() > 0){
			String error = "";
			for(String s : windowsApp.getErrors()){
				error += s + "\n";
			}
			JLabel errors = new JLabel(error);
			errors.setBounds(0, 0, 100, 44);
		}
		
		lblUsername = new JLabel("Username:");
		lblUsername.setBounds(245, 250, 76, 22);
		pnlLogin.add(lblUsername);
		
		lblPassword = new JLabel("Password:");
		lblPassword.setBounds(245, 288, 77, 16);
		pnlLogin.add(lblPassword);
		
		txtPassword = new JPasswordField();
		txtPassword.setBounds(314, 285, 241, 22);
		pnlLogin.add(txtPassword);
		
		txtUsername = new JTextField();
		txtUsername.setBounds(314, 250, 241, 22);
		pnlLogin.add(txtUsername);
		
		JButton btnLogin = new JButton("Login");
		btnLogin.setBounds(582, 528, 97, 58);
		btnLogin.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String username = txtUsername.getText();
				
				User user = windowsApp.getCSEP().getConnectionServiceImpPort().getUser(username);
				if(!user.getUsername().equals("NOTFOUND")){
					char[] passwordarray = txtPassword.getPassword();
					String password = "";
					for(char c : passwordarray){
						password += c;
					}
					
					if(Encryption.digest(password).equals(user.getPassword())){
						//good login
						windowsApp.setUser(user);
						windowsApp.deviceAttachedUser();
//						windowsApp.getDeviceInfo();
//						windowsApp.startFileService();
						
						windowsApp.getFrame().setContentPane(new Overview(windowsApp).getPanel());
					}else{
						//bad login
						String error = "Invalid password or username.";
						windowsApp.addError(error);
						windowsApp.getFrame().setContentPane(new Login(windowsApp).getPanel());
					}
				}else{
					//username not found, bad login
					String error = "Invalid username or password";
					windowsApp.addError(error);
					windowsApp.getFrame().setContentPane(new Login(windowsApp).getPanel());
				}
				
			}
		});
		pnlLogin.add(btnLogin);
		
		JButton btnRegister = new JButton("Register");
		btnRegister.setBounds(691, 527, 97, 25);
		btnRegister.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				windowsApp.getFrame().setContentPane(new CreateAccount(windowsApp).getPanel());
			}
		});
		pnlLogin.add(btnRegister);
		
		JButton btnRecover = new JButton("Recover");
		btnRecover.setBounds(691, 562, 97, 25);
		btnRecover.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
//				windowsApp.getFrame().setContentPane(new RecoverAccount(windowsApp).getPanel());
				System.out.println("RECOVER CLIKED");
			}
		});
		pnlLogin.add(btnRecover);
	}
	
	public JPanel getPanel(){
		return pnlLogin;
	}
}
